{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 函数def是一个操作的集合"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 函数语法："
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "def 函数名（参数1，参数2，...):\n",
    "    操作1\n",
    "    操作2\n",
    "    操作3\n",
    "    。。。。。。\n",
    "    return 值  #返回值"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 1.简单函数定义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "def demosss():\n",
    "    for i in range(10):\n",
    "        print('当前值：', i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "当前值： 0\n",
      "当前值： 1\n",
      "当前值： 2\n",
      "当前值： 3\n",
      "当前值： 4\n",
      "当前值： 5\n",
      "当前值： 6\n",
      "当前值： 7\n",
      "当前值： 8\n",
      "当前值： 9\n"
     ]
    }
   ],
   "source": [
    "demosss()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.函数的参数使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sums(a, b):\n",
    "    sums = 0\n",
    "    for i in range(a, b):\n",
    "        sums = sums + i\n",
    "    print('总和是：',sums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "总和是： 3\n"
     ]
    }
   ],
   "source": [
    "sums(0,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sums2(a, b):\n",
    "    sums = 0\n",
    "    for i in range(a, b+1):\n",
    "        sums = sums + i\n",
    "    print('总和是：',sums)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "总和是： 6\n"
     ]
    }
   ],
   "source": [
    "sums2(0,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "总和是： 5050\n"
     ]
    }
   ],
   "source": [
    "sums2(1,100)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "总和是： 50005000\n"
     ]
    }
   ],
   "source": [
    "sums(1,10000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3.函数的返回值 return"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sums2(a, b):\n",
    "    sums = 0\n",
    "    for i in range(a, b+1):\n",
    "        sums = sums + i\n",
    "    return sums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "s = sums2(1,3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    }
   ],
   "source": [
    "print(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "结果： 338250\n"
     ]
    }
   ],
   "source": [
    "demo = 0\n",
    "for i in range(1,100):\n",
    "    # 1至100总和 + 2至100总和 + 3至100总和 + ....... + 99至100总和\n",
    "    demo = demo + sums2(i,100)\n",
    "print('结果：',demo)\n",
    "# 5050 + 5049 + 5047 + ... ..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4.函数的默认参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sums3(a, b=1000):   # 有默认值的参数必须在后面，无默认值的参数必须在前面\n",
    "    sums = 0\n",
    "    for i in range(a, b+1):\n",
    "        sums = sums + i\n",
    "    return sums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    }
   ],
   "source": [
    "print(sums3(1,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "500500\n"
     ]
    }
   ],
   "source": [
    "print(sums3(1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "non-default argument follows default argument (<ipython-input-29-119abb97009b>, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-29-119abb97009b>\"\u001b[1;36m, line \u001b[1;32m1\u001b[0m\n\u001b[1;33m    def sums3(a=1000, b):   # 有默认值的参数必须在后面，无默认值的参数必须在前面\u001b[0m\n\u001b[1;37m             ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m non-default argument follows default argument\n"
     ]
    }
   ],
   "source": [
    "def sums3(a=1000, b):   # 有默认值的参数必须在后面，无默认值的参数必须在前面\n",
    "    sums = 0\n",
    "    for i in range(a, b+1):\n",
    "        sums = sums + i\n",
    "    return sums"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 5.顺序赋值和指定赋值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "def sums4(a, c, b=1000):   # 有默认值的参数必须在后面，无默认值的参数必须在前面\n",
    "    sums = 0\n",
    "    for i in range(a, b+1):\n",
    "        sums = sums + i\n",
    "    return sums+c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "8"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sums4(1,2,3) # 顺序赋值  a=1 , c=2, b=3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sums4(a=1,b=2,c=3) # 指定赋值  a=1 , c=2, b=3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1: 501000\n",
      "2: 501000\n",
      "3: 126250\n"
     ]
    }
   ],
   "source": [
    "print('1:', sums4(1,500,1000))\n",
    "print('2:', sums4(a=1,c=500,b=1000))\n",
    "\n",
    "print('3:', sums4(a=1,b=500,c=1000))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
